<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PDF右侧白边问题解决方案</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        body {
            background: linear-gradient(135deg, #2c3e50, #1a2530);
            color: #333;
            padding: 20px;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        
        .container {
            max-width: 1000px;
            width: 100%;
            margin: 20px auto;
            background: white;
            border-radius: 12px;
            box-shadow: 0 8px 30px rgba(0, 0, 0, 0.3);
            overflow: hidden;
        }
        
        header {
            background: #1a365d;
            color: white;
            padding: 25px;
            text-align: center;
        }
        
        h1 {
            font-size: 2.5rem;
            margin-bottom: 10px;
        }
        
        .subtitle {
            font-size: 1.2rem;
            opacity: 0.9;
        }
        
        .content {
            padding: 30px;
        }
        
        .section {
            margin-bottom: 30px;
        }
        
        h2 {
            color: #1a365d;
            margin-bottom: 15px;
            padding-bottom: 10px;
            border-bottom: 2px solid #e2e8f0;
        }
        
        p {
            line-height: 1.6;
            margin-bottom: 15px;
            font-size: 1.05rem;
        }
        
        .code-block {
            background: #f8fafc;
            border: 1px solid #e2e8f0;
            border-radius: 6px;
            padding: 18px;
            margin: 20px 0;
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 0.95rem;
            line-height: 1.5;
            overflow-x: auto;
        }
        
        .problem-solution {
            display: flex;
            gap: 20px;
            margin: 25px 0;
        }
        
        .problem, .solution {
            flex: 1;
            padding: 20px;
            border-radius: 8px;
        }
        
        .problem {
            background: #fff5f5;
            border: 1px solid #fed7d7;
        }
        
        .solution {
            background: #f0fff4;
            border: 1px solid #c6f6d5;
        }
        
        .preview {
            background: #4a5568;
            padding: 25px;
            border-radius: 8px;
            color: white;
            margin: 25px 0;
        }
        
        .a5-page {
            width: 148mm;
            height: 210mm;
            margin: 0 auto;
            background: white;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
            position: relative;
            overflow: hidden;
            border: 1px solid #cbd5e0;
        }
        
        .page-content {
            padding: 15mm;
            height: 100%;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
        }
        
        .page-header {
            text-align: center;
            padding-bottom: 10px;
            border-bottom: 2px solid #e53e3e;
        }
        
        .page-title {
            font-size: 24px;
            font-weight: bold;
            color: #1a365d;
        }
        
        .page-body {
            flex-grow: 1;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            text-align: center;
        }
        
        .main-heading {
            font-size: 32px;
            font-weight: 800;
            color: #1a365d;
            margin-bottom: 15px;
        }
        
        .accent {
            color: #e53e3e;
        }
        
        .subheading {
            font-size: 18px;
            color: #4a5568;
            margin-bottom: 25px;
        }
        
        .badge {
            background: #e53e3e;
            color: white;
            padding: 10px 25px;
            border-radius: 25px;
            font-weight: bold;
            display: inline-block;
        }
        
        .page-footer {
            text-align: center;
            font-size: 14px;
            color: #718096;
        }
        
        .print-controls {
            display: flex;
            gap: 15px;
            margin: 30px 0;
        }
        
        button {
            padding: 12px 25px;
            border: none;
            border-radius: 6px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
        }
        
        .print-btn {
            background: #3182ce;
            color: white;
        }
        
        .print-btn:hover {
            background: #2c5282;
        }
        
        .test-btn {
            background: #38a169;
            color: white;
        }
        
        .test-btn:hover {
            background: #2f855a;
        }
        
        footer {
            text-align: center;
            margin-top: 40px;
            color: #a0aec0;
            font-size: 0.9rem;
        }
        
        @media (max-width: 768px) {
            .problem-solution {
                flex-direction: column;
            }
            
            h1 {
                font-size: 2rem;
            }
        }
        
        /* 打印样式 */
        @media print {
            body, html {
                width: 100%;
                height: 100%;
                margin: 0;
                padding: 0;
                background: white;
            }
            
            .container {
                width: 100%;
                max-width: 100%;
                margin: 0;
                box-shadow: none;
                border-radius: 0;
            }
            
            .no-print {
                display: none;
            }
            
            .a5-page {
                box-shadow: none;
                border: none;
                margin: 0;
                width: 148mm;
                height: 210mm;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>PDF右侧白边问题解决方案</h1>
            <p class="subtitle">分析与解决HTML转PDF时出现的右侧白边问题</p>
        </header>
        
        <div class="content">
            <div class="section">
                <h2>问题描述</h2>
                <p>在将HTML内容导出为PDF时，右侧经常会出现不需要的白边。这个问题通常是由于页面设置、CSS样式或打印配置不正确导致的。</p>
                
                <div class="problem-solution">
                    <div class="problem">
                        <h3>常见原因</h3>
                        <ul>
                            <li>页面尺寸设置不正确</li>
                            <li>元素宽度超过页面可打印区域</li>
                            <li>默认页边距未正确重置</li>
                            <li>打印样式表缺失或错误</li>
                            <li>浏览器打印功能的默认设置</li>
                        </ul>
                    </div>
                    
                    <div class="solution">
                        <h3>解决方案</h3>
                        <ul>
                            <li>使用正确的@page规则设置尺寸和边距</li>
                            <li>确保内容宽度不超过页面宽度</li>
                            <li>在打印样式中重置所有边距</li>
                            <li>使用print-color-adjust确保背景色打印</li>
                            <li>测试不同浏览器的打印效果</li>
                        </ul>
                    </div>
                </div>
            </div>
            
            <div class="section">
                <h2>A5页面预览</h2>
                <p>下面是一个正确配置的A5页面示例，注意它没有右侧白边：</p>
                
                <div class="a5-page">
                    <div class="page-content">
                        <div class="page-header">
                            <div class="page-title">SwiftUI 100天教程</div>
                        </div>
                        
                        <div class="page-body">
                            <h1 class="main-heading">SwiftUI <span class="accent">100天</span></h1>
                            <p class="subheading">掌握iOS应用开发的终极指南</p>
                            <div class="badge">100 DAYS OF SWIFTUI - 中文版</div>
                        </div>
                        
                        <div class="page-footer">
                            <p>由 SwiftUI训练营 翻译整理</p>
                        </div>
                    </div>
                </div>
                
                <div class="print-controls">
                    <button class="print-btn" onclick="window.print()">打印预览</button>
                    <button class="test-btn" onclick="testPageSettings()">测试页面设置</button>
                </div>
            </div>
            
            <div class="section">
                <h2>正确的打印CSS配置</h2>
                <p>以下CSS代码可以帮助消除PDF右侧白边：</p>
                
                <div class="code-block">
/* 设置A5页面尺寸和边距 */
@page {
    size: A5;
    margin: 0;
}

/* 确保HTML和Body元素没有默认边距 */
@media print {
    html, body {
        width: 148mm;
        height: 210mm;
        margin: 0 !important;
        padding: 0 !important;
    }
    
    /* 确保容器元素填满整个页面 */
    .a5-page {
        width: 148mm;
        height: 210mm;
        margin: 0;
        padding: 0;
        box-shadow: none;
        border: none;
    }
    
    /* 强制打印背景颜色和图像 */
    * {
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }
}
                </div>
                
                <p>确保在打印媒体查询中重置所有相关元素的边距和填充，并明确设置尺寸。</p>
            </div>
            
            <div class="section">
                <h2>其他注意事项</h2>
                <p>除了CSS配置，还需要注意以下几点：</p>
                
                <div class="code-block">
/* 使用精确的尺寸单位 */
width: 148mm;   /* 而不是px或百分比 */
height: 210mm;  /* A5尺寸的标准高度 */

/* 避免使用可能导致溢出的属性 */
white-space: nowrap;
overflow: hidden;

/* 确保盒子模型计算正确 */
box-sizing: border-box;

/* 检查所有父元素是否有限制宽度 */
max-width: 100%;
                </div>
                
                <p>在不同浏览器中测试打印效果，因为每个浏览器的打印实现可能略有不同。</p>
            </div>
        </div>
    </div>
    
    <footer class="no-print">
        <p>© 2023 PDF打印问题解决方案 | 页面设计用于演示目的</p>
    </footer>

    <script>
        function testPageSettings() {
            alert('测试页面设置：\n1. 确保@page规则正确设置\n2. 检查所有元素的盒模型\n3. 验证打印媒体查询中的样式');
        }
    </script>
</body>
</html>